<?php

/**
 * Description of FileGenerator
 *
 * @author David Safranek <safrad@rosada.cz>
 */

class FileGenerator extends FileGeneratorAbstract {

	/**
	 * Update target file
	 * @return boolean
	 * @throws Exception
	 */
	protected function forceGenerate() {
		$lastDate = $this->targetDate();
		$errorCode = exec($this->getCommand());
		if ($errorCode != 0) {
			throw new Exception('Command error code: ' . $errorCode);
		}
		if (!file_exists($this->getTarget())) {
			throw new Exception($this->getTarget() . ' is NOT created!' . "\n" . 'Command: ' . $this->getCommand());
		}

		// filemtime is cached, we have to clean this file cache entry
		clearstatcache();
		// clearstatcache(true, $this->getTarget()); // Since PHP 5.3.0
		
		if (($this->targetDate() == $lastDate)) {
			throw new Exception('Generated target file (' . $this->getTarget() . ') time (' . gmdate("Y-m-d\TH:i:s\Z",$lastDate) . ') is not changed!');
		}
	}
	
}
